package com.example.demo.controller;

import com.example.demo.entity.EavAttribute;
import com.example.demo.entity.EavEntityType;
import com.example.demo.entity.Goods;
import com.example.demo.service.EavAttributeService;
import com.example.demo.service.EavEntityTypeService;
import com.example.demo.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    @Resource
    private EavEntityTypeService eavEntityTypeService;
    @Resource
    private EavAttributeService eavAttributeService;
    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping
    public ResponseEntity<?> list() throws IllegalAccessException {
        List<Goods> list = goodsService.list();
        List<Map<String, Object>> result = new ArrayList<>();
        for (Goods goods:list
             ) {
            Map<String, Object> data = new HashMap<>();
            Field[] declaredFields = goods.getClass().getDeclaredFields();
            for (Field field:declaredFields
                 ) {
                field.setAccessible(true);
                data.put(field.getName(), field.get(goods));
            }

            EavEntityType eavEntityType = eavEntityTypeService.selectByEntityModel(goods.getId(), Goods.class.getSimpleName());
            if (eavEntityType != null){
                List<EavAttribute> eavAttributes = eavAttributeService.selectByTypeId(eavEntityType.getId());
                if (eavAttributes != null && !eavAttributes.isEmpty()){
                    for (EavAttribute eavAttribute:eavAttributes
                         ) {
                        data.put(eavAttribute.getAttributeCode(), 0);
                    }
                }
            }
            result.add(data);
        }


        return ResponseEntity.ok(result);
    }

    @GetMapping("create")
    public ResponseEntity<?> addColumns(HttpServletRequest request){
        String column = request.getParameter("column");
        if (StringUtils.isBlank(column)){
            return ResponseEntity.ok("新增字段名不能为空");
        }

        String type = request.getParameter("type");
        if (StringUtils.isBlank(type)){
            return ResponseEntity.ok("新增字段类型不能为空");
        }

        String entityId = request.getParameter("entity_id");
        if (StringUtils.isBlank(entityId)){
            return ResponseEntity.ok("实体类id不能为空");
        }

        EavEntityType eavEntityType = eavEntityTypeService.selectByEntityModel(Long.valueOf(entityId), Goods.class.getSimpleName());
        if (eavEntityType == null) {
            eavEntityType = new EavEntityType();
            eavEntityType.setEntityId(Long.valueOf(entityId));
            eavEntityType.setEntityTypeCode(Goods.class.getName());
            eavEntityType.setEntityModel(Goods.class.getName());
            eavEntityTypeService.save(eavEntityType);
        }

        EavAttribute eavAttribute = new EavAttribute();
        eavAttribute.setEntityTypeId(eavEntityType.getId());
        eavAttribute.setAttributeCode(column);
        eavAttribute.setBackendType(type);
        eavAttributeService.save(eavAttribute);
        return ResponseEntity.ok("新增成功");
    }


    @GetMapping("shop/{id}/{num}")
    public ResponseEntity<?> shop(@PathVariable Long id, @PathVariable Integer num){
        Goods goods = goodsService.getById(id);
        if (goods == null) return ResponseEntity.ok("不存在无货");
        int newStock = goods.getStock()-num;
        log.info(">>>>>>>>>>>>>>>货物目前："+goods.getStock());
        if (newStock < 0) {
            return ResponseEntity.ok("货物目前："+goods.getStock()+"不足");
        }
        goods.setStock(newStock);
        boolean updateById = goodsService.updateById(goods);
        if (!updateById)return ResponseEntity.ok("更新失败");
        return ResponseEntity.ok("抢购成功");
    }


}
